/* See the file "LICENSE" for the full license governing this code. */
package com.plugandcare.graphyty.gui;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;

import com.plugandcare.data.VitalSignProviderContants;
import com.plugandcare.graphyty.R;

/**
 * @author Timo Tischler
 * @author Steven Mohr
 * 
 */
public class BodyWeightActivity extends AbstractChartActivity {

    private static final ChartType CHART_TYPE = ChartType.BODY_WEIGHT;

    private ContentResolver cr = null;

    private Cursor c = null;

    @Override
    public void onStart() {
        super.onStart();
        refresh();
    }

    private List<double[]> getWeightValues() {
        List<double[]> valuesAL = null;

        if (c != null) {
            valuesAL = new ArrayList<double[]>();
            double[] valuesArray = new double[c.getCount()];
            c.moveToFirst();
            for (int i = 0; i < c.getCount(); i++) {
                valuesArray[i] = c.getDouble(c.getColumnIndex(VitalSignProviderContants.WEIGHT));
                c.moveToNext();
            }
            valuesAL.add(valuesArray);
        } 
        return valuesAL;
    }

    @Override
    protected void refresh() {
        double[][] hoursAndMinutes = getHoursAndMinutes(CHART_TYPE);

        cr = getContentResolver();
        c = cr.query(VitalSignProviderContants.BodyWeightColumns.CONTENT_URI, VitalSignProviderContants.BodyWeightColumns.COLUMN_NAMES,
                null, null, null);

        double[] xArray = null;
        if (c != null) {
            xArray = new double[c.getCount()];
            c.moveToFirst();
            for (int i = 0; i < c.getCount(); i++) {
                xArray[i] = hoursAndMinutes[0][i] + hoursAndMinutes[1][i];
            }

            List<double[]> x = new ArrayList<double[]>();

            x.add(xArray);

            List<double[]> weightValues = new ArrayList<double[]>();
            weightValues = getWeightValues();

            XYMultipleSeriesRenderer renderer = createRenderer(1);
            XYMultipleSeriesDataset dataset = buildDataset(new String[] { getString(R.string.body_weight) }, x, weightValues);
            setChartSettings(renderer, getString(R.string.body_weight), getString(R.string.time), getString(R.string.unit_kg),
                getMin(hoursAndMinutes[0]), getMax(hoursAndMinutes[0]) + 2, 0, 150,
                Color.LTGRAY, Color.LTGRAY);
            renderer.setXLabels((int) (getMax(hoursAndMinutes[0]) - getMin(hoursAndMinutes[0]) + 2));

            setContentView(ChartFactory.getLineChartView(this, dataset, renderer));
        } else {
            createEmptyChart(CHART_TYPE);
        }
    }

}
